// var n;
// var $pPSDK_Config = require("./AL_Framework_AL_SDK_CONFIG");
// var $macros = require("./AL_Framework_Macros");
// var $fitScale = require("./AL_Framework_Fit_Scale");
// var d = cc._decorator;
// var u = d.ccclass;
// var h = d.property;
// var p = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.bg = null;
//         t.window = null;
//         t.label_Node = null;
//         t.title_label = null;
//         t.content_Label = null;
//         t.button_Node = null;
//         t.node_button_cancel = null;
//         t.confirm_Button = null;
//         t.confirm_Label = null;
//         t.cancel_Label = null;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.onLoad = function () {
//         this.node.addComponent($fitScale.default).shezhiShifouShiyingKuanGao(!0, !1);
//     };
//     t.prototype.show = function (e) {
//         var t = this;
//         this.node.active = !0;
//         if ("" === e.title) {
//             this.title_label.node.active = !1;
//         }
//         this.node_button_cancel.active = e.showCancel;
//         this.title_label.string = e.title;
//         this.content_Label.string = e.content;
//         this.cancel_Label.string = e.cancelText;
//         this.confirm_Label.string = e.confirmText;
//         this.bg.opacity = 0;
//         cc.tween(this.bg)
//             .to(0.2, {
//                 opacity: 100
//             })
//             .start();
//         this.window.opacity = 0;
//         this.scheduleOnce(function () {
//             t.button_Node.width = Math.max(t.label_Node.width, 500);
//             t.window.opacity = 255;
//         }, 0.05);
//         this.node_button_cancel.on(
//             "touchend",
//             function () {
//                 if (e.cancel) {
//                     e.cancel();
//                 }
//             },
//             this
//         );
//         this.confirm_Button.on(
//             "touchend",
//             function () {
//                 if (e.confirm) {
//                     e.confirm();
//                 }
//             },
//             this
//         );
//     };
//     t.prototype.close = function () {
//         this.node.active = !1;
//         return Promise.resolve();
//     };
//     __decorate([h(cc.Node)], t.prototype, "bg", void 0);
//     __decorate([h(cc.Node)], t.prototype, "window", void 0);
//     __decorate([h(cc.Node)], t.prototype, "label_Node", void 0);
//     __decorate([h(cc.Label)], t.prototype, "title_label", void 0);
//     __decorate([h(cc.Label)], t.prototype, "content_Label", void 0);
//     __decorate([h(cc.Node)], t.prototype, "button_Node", void 0);
//     __decorate([h(cc.Node)], t.prototype, "node_button_cancel", void 0);
//     __decorate([h(cc.Node)], t.prototype, "confirm_Button", void 0);
//     __decorate([h(cc.Label)], t.prototype, "confirm_Label", void 0);
//     __decorate([h(cc.Label)], t.prototype, "cancel_Label", void 0);
//     return __decorate([u], t);
// })(cc.Component);
// var g = (function () {
//     function e() {
//         this.logTag = "[AL_Framework_Alert]";
//         this._prefab = null;
//         this._queue = [];
//         this.curPanel = null;
//     }
//     e.Instance = function () {
//         return this._instance || (this._instance = new e());
//     };
//     e.prototype.jiazai_yuzhiti = function () {
//         var e = this;
//         return new Promise(function (t, o) {
//             var i = "HUA/framework/common/components/alert/modalNode";
//             Manager.cacheManager.yibuHuoquHuancun(i, cc.Prefab, $macros.Macro.BUNDLE_RESOURCES).then(function (n) {
//                 n
//                     ? (Manager.assetManager.addPersistAsset(i, n, $macros.Macro.BUNDLE_RESOURCES),
//                       e.shezhi_yuzhiti(n, p),
//                       t(n))
//                     : (Log.e(e.logTag, "AL_Framework_Alert 预制体加载失败:" + i), o());
//             });
//         });
//     };
//     e.prototype.shezhi_yuzhiti = function (e, t) {
//         this._huoqu_yuzhiti_xinxi(e).getComponent(t)
//             ? ((this._type = t), (this._prefab = e))
//             : Log.e(this.logTag, "未挂载" + t.prototype.constructor.name + ",将无法使用，请排查");
//     };
//     e.prototype.init = function () {
//         return $pPSDK_Config.Config.USE_SYS_ALERT ? this.jiazai_yuzhiti() : Promise.resolve();
//     };
//     e.prototype.show = function (e) {
//         return e.tag && e.shifou_chongfu_xianshi_tishikuang && this.shifou_chongfu_xianshi_tishikuang(e.tag)
//             ? (Log.w(this.logTag, "弹出框已经存在 config : " + e.tag), !1)
//             : (this._queue.push(e), this._xianshi(e), !0);
//     };
//     e.prototype.dangqian_shifou_xianshi_zhiding_tishikuang = function (e) {
//         return !(!this.curPanel || this.curPanel.config.tag != e);
//     };
//     e.prototype.huoqu_dangqian_xianshi_tishikuang = function (e) {
//         if (this.curPanel) {
//             var t = this.curPanel.config;
//             if (!e) {
//                 return t;
//             }
//             if (t.tag == e) {
//                 return t;
//             }
//         }
//         return null;
//     };
//     e.prototype.shifou_chongfu_xianshi_tishikuang = function (e) {
//         if (this.curPanel) {
//             if (this.curPanel.config.config.tag == e) {
//                 Log.w(this.logTag, "重复的弹出框 config ; " + e);
//                 return !0;
//             }
//         } else {
//             for (var t = 0; t < this._queue.length; t++) {
//                 if (this._queue[t].tag == e) {
//                     Log.w(this.logTag, "重复的弹出框 config ; " + e);
//                     return !0;
//                 }
//             }
//         }
//         return !1;
//     };
//     e.prototype.close = function (e) {
//         if (e) {
//             for (var t = this._queue.length; t--; ) {
//                 if (this._queue[t].tag == e) {
//                     this._queue.splice(t, 1);
//                 }
//             }
//             if (this.curPanel && this.curPanel.config.tag == e) {
//                 this.wancheng_tishikuang_xianshi();
//             }
//         } else {
//             this.wancheng_tishikuang_xianshi();
//         }
//     };
//     e.prototype.guanbi_suoyou = function () {
//         this._queue = [];
//         this.wancheng_tishikuang_xianshi();
//     };
//     e.prototype.wancheng_tishikuang_xianshi = function () {
//         if (this.curPanel) {
//             var e = this.curPanel.getComponent(this._type);
//             var t = e.close();
//             t instanceof Promise
//                 ? t.then(function () {
//                       Manager.nodePoolManager.huoqu_pool("sys_alert").put(e.node);
//                   })
//                 : Manager.nodePoolManager.huoqu_pool("sys_alert").put(e.node);
//             this.curPanel = null;
//         }
//         var o = this._queue.shift();
//         return 0 != this._queue.length ? (this._xianshi(this._queue[0]), this._queue[0]) : o;
//     };
//     e.prototype._xianshi = function (e) {
//         if (this._prefab) {
//             if (!this.curPanel) {
//                 var t = Manager.nodePoolManager.huoqu_pool("sys_alert", !0, this._prefab).get();
//                 var o = t.getComponent(this._type);
//                 Manager.uiManager.tianjia_shitu(t, 50);
//                 o.show(this._huoqu_peizhi(e));
//                 t.config = e;
//                 this.curPanel = t;
//             }
//         } else {
//             Log.e(this.logTag, "未加载系统Alert组件,tag:", e.tag);
//         }
//     };
//     e.prototype._huoqu_peizhi = function (e) {
//         var t = this;
//         var o = Manager.utils.qian_kaobei(e);
//         if (void 0 === o.showCancel) {
//             o.showCancel = !0;
//         }
//         if (!o.confirmText) {
//             o.confirmText = Manager.languageManager.huoqu_biaoqian_neirong("framework", "确定");
//         }
//         if (!o.cancelText) {
//             o.cancelText = Manager.languageManager.huoqu_biaoqian_neirong("framework", "取消");
//         }
//         if (void 0 === o.shifou_chongfu_xianshi_tishikuang) {
//             o.shifou_chongfu_xianshi_tishikuang = !0;
//         }
//         var i = o.confirm;
//         o.confirm = function () {
//             if (i) {
//                 i();
//             }
//             t.close(o.tag);
//         };
//         var n = o.cancel;
//         o.cancel = function () {
//             if (n) {
//                 n();
//             }
//             t.close(o.tag);
//         };
//         return o;
//     };
//     e.prototype._huoqu_yuzhiti_xinxi = function (e) {
//         return e instanceof cc.Prefab ? e.data : e;
//     };
//     e._instance = null;
//     return e;
// })();
// exports.default = g;


const AL_Framework_AL_SDK_CONFIG = require("./AL_Framework_AL_SDK_CONFIG");
const AL_Framework_Macros = require("./AL_Framework_Macros");
const AL_Framework_Fit_Scale = require("./AL_Framework_Fit_Scale");
const { ccclass, property } = cc._decorator;

/**
 * 提示框组件类
 */
@ccclass
class AlertPanel extends cc.Component {
    @property(cc.Node) bg = null;
    @property(cc.Node) window = null;
    @property(cc.Node) label_Node = null;
    @property(cc.Label) title_label = null;
    @property(cc.Label) content_Label = null;
    @property(cc.Node) button_Node = null;
    @property(cc.Node) node_button_cancel = null;
    @property(cc.Node) confirm_Button = null;
    @property(cc.Label) confirm_Label = null;
    @property(cc.Label) cancel_Label = null;

    onLoad() {
        this.node.addComponent(AL_Framework_Fit_Scale.default).shezhiShifouShiyingKuanGao(true, false);
    }

    /**
     * 显示提示框
     * @param {Object} config 配置对象
     */
    show(config) {
        this.node.active = true;
        if (config.title === "") {
            this.title_label.node.active = false;
        }
        this.node_button_cancel.active = config.showCancel;
        this.title_label.string = config.title;
        this.content_Label.string = config.content;
        this.cancel_Label.string = config.cancelText;
        this.confirm_Label.string = config.confirmText;
        this.bg.opacity = 0;

        cc.tween(this.bg)
            .to(0.2, { opacity: 100 })
            .start();

        this.window.opacity = 0;
        this.scheduleOnce(() => {
            this.button_Node.width = Math.max(this.label_Node.width, 500);
            this.window.opacity = 255;
        }, 0.05);

        this.node_button_cancel.on("touchend", () => {
            if (config.cancel) {
                config.cancel();
            }
        }, this);

        this.confirm_Button.on("touchend", () => {
            if (config.confirm) {
                config.confirm();
            }
        }, this);
    }

    /**
     * 关闭提示框
     * @returns {Promise<void>}
     */
    close() {
        this.node.active = false;
        return Promise.resolve();
    }
}

exports.default = AlertPanel;

/**
 * 提示框管理器类
 */
class AlertManager {
    constructor() {
        this.logTag = "[AL_Framework_Alert]";
        this._prefab = null;
        this._queue = [];
        this.curPanel = null;
    }

    /**
     * 获取单例实例
     * @returns {AlertManager}
     */
    static Instance() {
        return this._instance || (this._instance = new AlertManager());
    }

    /**
     * 加载预制体
     * @returns {Promise<cc.Prefab>}
     */
    jiazai_yuzhiti() {
        return new Promise((resolve, reject) => {
            const path = "HUA/framework/common/components/alert/modalNode";

            Manager.cacheManager.yibuHuoquHuancun(path, cc.Prefab, AL_Framework_Macros.Macro.BUNDLE_RESOURCES)
                .then(prefab => {
                    if (prefab) {
                        Manager.assetManager.addPersistAsset(path, prefab, AL_Framework_Macros.Macro.BUNDLE_RESOURCES);
                        this.shezhi_yuzhiti(prefab, AlertPanel);
                        resolve(prefab);
                    } else {
                        Log.e(this.logTag, `AL_Framework_Alert 预制体加载失败: ${path}`);
                        reject();
                    }
                });
        });
    }

    /**
     * 设置预制体
     * @param {cc.Prefab} prefab 预制体
     * @param {Function} type 组件类型
     */
    shezhi_yuzhiti(prefab, type) {
        if (this._huoqu_yuzhiti_xinxi(prefab).getComponent(type)) {
            this._type = type;
            this._prefab = prefab;
        } else {
            Log.e(this.logTag, `未挂载${type.prototype.constructor.name}, 将无法使用，请排查`);
        }
    }

    /**
     * 初始化提示框管理器
     * @returns {Promise<void>}
     */
    init() {
        return AL_Framework_AL_SDK_CONFIG.Config.USE_SYS_ALERT ? this.jiazai_yuzhiti() : Promise.resolve();
    }

    /**
     * 显示提示框
     * @param {Object} config 配置对象
     * @returns {boolean}
     */
    show(config) {
        if (config.tag && config.shifou_chongfu_xianshi_tishikuang && this.shifou_chongfu_xianshi_tishikuang(config.tag)) {
            Log.w(this.logTag, `弹出框已经存在 config : ${config.tag}`);
            return false;
        } else {
            this._queue.push(config);
            this._xianshi(config);
            return true;
        }
    }

    /**
     * 检查当前是否显示指定标签的提示框
     * @param {string} tag 标签
     * @returns {boolean}
     */
    dangqian_shifou_xianshi_zhiding_tishikuang(tag) {
        return this.curPanel && this.curPanel.config.tag === tag;
    }

    /**
     * 获取当前显示的提示框配置
     * @param {string} tag 标签
     * @returns {Object|null}
     */
    huoqu_dangqian_xianshi_tishikuang(tag) {
        if (this.curPanel) {
            const config = this.curPanel.config;
            if (!tag || config.tag === tag) {
                return config;
            }
        }
        return null;
    }

    /**
     * 检查是否重复显示提示框
     * @param {string} tag 标签
     * @returns {boolean}
     */
    shifou_chongfu_xianshi_tishikuang(tag) {
        if (this.curPanel) {
            if (this.curPanel.config.config.tag === tag) {
                Log.w(this.logTag, `重复的弹出框 config ; ${tag}`);
                return true;
            }
        } else {
            for (let i = 0; i < this._queue.length; i++) {
                if (this._queue[i].tag === tag) {
                    Log.w(this.logTag, `重复的弹出框 config ; ${tag}`);
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 关闭指定标签的提示框
     * @param {string} tag 标签
     */
    close(tag) {
        if (tag) {
            for (let i = this._queue.length - 1; i >= 0; i--) {
                if (this._queue[i].tag === tag) {
                    this._queue.splice(i, 1);
                }
            }
            if (this.curPanel && this.curPanel.config.tag === tag) {
                this.wancheng_tishikuang_xianshi();
            }
        } else {
            this.wancheng_tishikuang_xianshi();
        }
    }

    /**
     * 关闭所有提示框
     */
    guanbi_suoyou() {
        this._queue = [];
        this.wancheng_tishikuang_xianshi();
    }

    /**
     * 完成提示框显示
     * @returns {Object|null}
     */
    wancheng_tishikuang_xianshi() {
        if (this.curPanel) {
            const panel = this.curPanel.getComponent(this._type);
            const closePromise = panel.close();

            if (closePromise instanceof Promise) {
                closePromise.then(() => {
                    Manager.nodePoolManager.huoqu_pool("sys_alert").put(panel.node);
                });
            } else {
                Manager.nodePoolManager.huoqu_pool("sys_alert").put(panel.node);
            }

            this.curPanel = null;
        }

        const nextConfig = this._queue.shift();
        if (this._queue.length > 0) {
            this._xianshi(this._queue[0]);
        }

        return nextConfig;
    }

    /**
     * 显示提示框
     * @param {Object} config 配置对象
     */
    _xianshi(config) {
        if (this._prefab) {
            if (!this.curPanel) {
                const panelNode = Manager.nodePoolManager.huoqu_pool("sys_alert", true, this._prefab).get();
                const panel = panelNode.getComponent(this._type);

                Manager.uiManager.tianjia_shitu(panelNode, 50);
                panel.show(this._huoqu_peizhi(config));

                panelNode.config = config;
                this.curPanel = panelNode;
            }
        } else {
            Log.e(this.logTag, `未加载系统Alert组件, tag: ${config.tag}`);
        }
    }

    /**
     * 获取配置对象
     * @param {Object} config 配置对象
     * @returns {Object} 配置对象
     */
    _huoqu_peizhi(config) {
        const copyConfig = Manager.utils.qian_kaobei(config);

        if (copyConfig.showCancel === undefined) {
            copyConfig.showCancel = true;
        }
        if (!copyConfig.confirmText) {
            copyConfig.confirmText = Manager.languageManager.huoqu_biaoqian_neirong("framework", "确定");
        }
        if (!copyConfig.cancelText) {
            copyConfig.cancelText = Manager.languageManager.huoqu_biaoqian_neirong("framework", "取消");
        }
        if (copyConfig.shifou_chongfu_xianshi_tishikuang === undefined) {
            copyConfig.shifou_chongfu_xianshi_tishikuang = true;
        }

        const confirmCallback = copyConfig.confirm;
        copyConfig.confirm = () => {
            if (confirmCallback) {
                confirmCallback();
            }
            this.close(copyConfig.tag);
        };

        const cancelCallback = copyConfig.cancel;
        copyConfig.cancel = () => {
            if (cancelCallback) {
                cancelCallback();
            }
            this.close(copyConfig.tag);
        };

        return copyConfig;
    }

    /**
     * 获取预制体信息
     * @param {cc.Prefab} prefab 预制体
     * @returns {Object}
     */
    _huoqu_yuzhiti_xinxi(prefab) {
        return prefab instanceof cc.Prefab ? prefab.data : prefab;
    }
}

AlertManager._instance = null;
exports.default = AlertManager;
